﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace 任务三
{
    class Program
    {
        static void Main(string[] args)
        {
            //读取E盘根目录下的文件夹和文件名及文件的大小，并把它们输出到E盘名为“统计.txt”的文档中
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"e:\");

            FileStream mystream = new FileStream(@"e:\统计.txt", FileMode.Append, FileAccess.Write);

            System.IO.StreamWriter objfile = new System.IO.StreamWriter(mystream);

            foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
            {
                Console.WriteLine("{0},{1}", file.FullName, file.Length);
                objfile.WriteLine("{0},{1}", file.FullName.ToString(), file.Length.ToString());
            }
            foreach (System.IO.DirectoryInfo file1 in dir.GetDirectories())
            {
                Console.WriteLine("{0}", file1.FullName);
                objfile.WriteLine("{0}", file1.FullName.ToString());
            }

            //遍历文件夹，把文件夹里面的所有txt文档复制到指定的文件夹中
            string sourceDirectory = @"e:\";
            string archiveDirectory = @"f:\achive";
            DirectoryInfo test = new DirectoryInfo(@"e:\测试文件夹");
            string destinationDirectory1 = @"e:\txt";
            string destinationDirectory2 = @"e:\jpg";
            string destinationDirectory3 = @"e:\ppt";

            try
            {
                var txtFiles = Directory.GetFiles(sourceDirectory, "*.txt");

                foreach (string currentFile in txtFiles)
                {
                    string fileName = currentFile.Substring(sourceDirectory.Length);
                    File.Copy(currentFile, Path.Combine(archiveDirectory, fileName));
                    Console.WriteLine("{0}已经成功复制到f:achive", fileName);
                    objfile.WriteLine("{0}已经成功复f:achive", fileName);
                }
                //遍历文件夹，把文件夹里面不同格式的文件分别移动到不同的文件夹里面，实现分类文件的功能
                foreach (FileInfo file1 in test.GetFiles("*.txt"))
                {

                    file1.MoveTo(Path.Combine(destinationDirectory1, file1.Name));
                }
                foreach (FileInfo file2 in test.GetFiles("*.jpg"))
                {

                    file2.MoveTo(Path.Combine(destinationDirectory2, file2.Name));
                }
                foreach (FileInfo file3 in test.GetFiles("*.ppt"))
                {
                    file3.MoveTo(Path.Combine(destinationDirectory3, file3.Name));
                }
           
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            objfile.Dispose();
            objfile.Close();

            Console.ReadLine();
        }
    }
}
